type SafeArea = UniApp.SafeArea | undefined
type NumOrUndef = number | undefined
export type WinInfo = { safeArea: SafeArea, screenWidth: number, screenHeight: number, windowWidth: number, windowHeight: number, windowTop: NumOrUndef, windowBottom: number, tabbarHeight: number, navigationHeight: number, statusBarHeight: NumOrUndef, getSystemInfo: UniApp.GetWindowInfoResult } | null
let winInfo: WinInfo = null
/**
 * 获取屏幕窗口安全高度和宽度
 * 注意是针对种屏幕的统一计算，统一高度，不再让uni获取有效高度而烦恼。
 * 请一定要在onMounted或者onLoad中调用，否则不准确在h5端。
 * @return {WinInfo} winInfo
 */
export function getWindow(reset?: boolean): WinInfo {
  if (reset) winInfo = null
  if (winInfo) return winInfo
  const getSystemInfo = uni.getWindowInfo()

  const tabbarHeight = 50
  const navigationHeight = 44
  const screenWidth = getSystemInfo.screenWidth
  const screenHeight = getSystemInfo.screenHeight

  const windowWidth = getSystemInfo.windowWidth
  const windowHeight = getSystemInfo.windowHeight
  const windowTop = getSystemInfo.windowTop
  const windowBottom = getSystemInfo.windowBottom

  const statusBarHeight = getSystemInfo.statusBarHeight

  winInfo = { safeArea: getSystemInfo.safeArea, screenWidth, screenHeight, windowWidth, windowHeight, tabbarHeight, navigationHeight, statusBarHeight, windowTop, windowBottom, getSystemInfo }
  return winInfo
}

type DeviceInfo = UniApp.GetDeviceInfoResult | null
/**
 * 获取屏幕窗口安全高度和宽度
 * 注意是针对种屏幕的统一计算，统一高度，不再让uni获取有效高度而烦恼。
 * 请一定要在onMounted或者onLoad中调用，否则不准确在h5端。
 * @return {DeviceInfo} deviceInfo 设备信息
 */
let deviceInfo: DeviceInfo = null
export function getDeviceInfo(reset?: boolean): DeviceInfo {
  if (reset) deviceInfo = null
  if (deviceInfo) return deviceInfo
  deviceInfo = uni.getDeviceInfo()
  return deviceInfo
}

/**
 * 节流
 * @param {Function} 执行函数
 * @param {number} delay 延时ms
 */
let _throttleRunning: boolean = false
export function throttle(fn: (arg?: any) => void, delay: number = 500) {
  if (_throttleRunning) {
    return
  }
  _throttleRunning = true
  fn()
  setTimeout(() => {
    _throttleRunning = false
  }, delay)
}

export const wxBtnRect = uni?.getMenuButtonBoundingClientRect?.() ?? { width: 0, height: 0, left: 0, right: 0, top: 0, bottom: 0 }
